package com.wswy.wzcx.ui.module.tels

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.che.libcommon.ui.adapter.SampleRecyclerViewAdapter
import com.wswy.wzcx.R
import com.wswy.wzcx.module.base.CBaseFragment
import com.wswy.wzcx.module.base.loadLayout
import com.wswy.wzcx.utils.Tools
import kotlinx.android.synthetic.main.f_layout_item_sos_tel.view.*

class TelListFragment: CBaseFragment() {

    companion object {

        fun newInstance(type:Int):Fragment{
            return TelListFragment().apply {

                arguments = Bundle().apply {
                    putInt("type",type)
                }

            }
        }

    }


    private lateinit var recyclerView: RecyclerView

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        container?:return null

        return RecyclerView(container.context).apply {
            id = R.id.recycler_view
            layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)
        }
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        recyclerView = view.findViewById(R.id.recycler_view)

        recyclerView.layoutManager = LinearLayoutManager(view.context)
        recyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
    }

    override fun getFragmentTitle(): String {
        return "应急电话"
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val type = arguments?.getInt("type")?:0


        val adapter = TelAdapter()

        adapter.setOnItemClickListener { _, _, t ->

            t?.run {

                Tools.startCallPhone(context,callNumber)

            }

        }

        recyclerView.adapter = adapter

        when(type){
            0 -> {
                adapter.setNewData(PhoneBook.roadServiceNumbers)
            }
            1 -> {
                adapter.setNewData(PhoneBook.insuranceCompanyNumbers)
            }
        }
        adapter.notifyDataSetChanged()
    }


    class TelAdapter : SampleRecyclerViewAdapter<SOSNumber,TelAdapter.TelViewHolder>(){

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TelViewHolder {
            return TelViewHolder(loadLayout(parent,R.layout.f_layout_item_sos_tel))
        }

        override fun onBindViewHolder(holder: TelViewHolder, position: Int, data: SOSNumber?) {

            holder.itemView.tv_tel_name.text = data?.name
            holder.itemView.tv_tel_display_tel.text = data?.displayNumber

        }


        class TelViewHolder(itemView:View) :RecyclerView.ViewHolder(itemView)

    }

}